package models;

import com.google.gson.JsonObject;
import helper.GlobalConfig;
import models.common.BaseModel;
import models.enums.SourceType;

import javax.persistence.*;

/**
 * 产品
 */
@Entity
@Table(name = "products", schema = GlobalConfig.MIGAO_DB_SCHEMA)
public class Product extends BaseModel{

    @Enumerated(EnumType.STRING)
    @Column(name = "source")
    public SourceType source;

    @Column(name = "goods_image_url")
    public String goodsImageUrl; //产品主图

    @Column(name = "goods_thumbnail_url")
    public String goodsThumbnailUrl; //产品缩略图

    @Column(name = "goods_name")
    public String goodsName;//产品标题

    @Column(name = "goods_id")
    public String goodsId;//多多进宝ID

    @Column(name = "goods_eval_score")
    public String goods_eval_score; //商品评价数量

    @Column(name = "")
    public String  goods_eval_count; //商品评价分

    @Column(name = "promotion_rate")
    public String promotion_rate; // 佣金比例 千分之

    @Column(name = "coupon_end_time")
    public String coupon_end_time; //优惠券失效时间，UNIX时间戳

    @Column(name = "coupon_start_time")
    public String coupon_start_time; //优惠券生效时间，UNIX时间戳

    @Column(name = "coupon_remain_quantity")
    public String coupon_remain_quantity;// 优惠券剩余数量

    @Column(name = "coupon_total_quantity")
    public String coupon_total_quantity; //优惠券总数量

    @Column(name = "coupon_discount")
    public String coupon_discount; //优惠券面额，单位为分

    @Column(name = "coupon_min_order_amount")
    public String coupon_min_order_amount; //优惠券门槛价格，单位为分

    @Column(name = "sold_quantity")
    public String sold_quantity; //已售卖件数

    public static Product findByGoodsId(String goodsId) {
         return Product.find("goodsId = ?1" , goodsId).first();
    }


    public static Product synProduct(JsonObject goodsDetail) {
        String goodsId = goodsDetail.get("goods_id").toString();
        Product product = Product.findByGoodsId(goodsId);
        if (product == null) {
            product = new Product();
            product.coupon_discount = goodsDetail.get("coupon_discount").toString();
            product.coupon_end_time = goodsDetail.get("coupon_end_time").toString();
            product.coupon_min_order_amount = goodsDetail.get("coupon_min_order_amount").toString();
            product.coupon_remain_quantity = goodsDetail.get("coupon_remain_quantity").toString();
            product.coupon_start_time = goodsDetail.get("coupon_start_time").toString();
            product.coupon_total_quantity = goodsDetail.get("coupon_total_quantity").toString();
            product.goods_eval_count = goodsDetail.get("goods_eval_count").toString();
            product.goodsId = goodsDetail.get("goods_id").toString();
            product.goodsImageUrl = goodsDetail.get("goods_image_url").toString();
            product.goodsName = goodsDetail.get("goods_name").toString();
            product.goodsThumbnailUrl = goodsDetail.get("goods_thumbnail_url").toString();
            product.source = SourceType.PINDUODUO;
            product.goods_eval_score = goodsDetail.get("goods_eval_score").toString();
            product.goods_eval_count = goodsDetail.get("goods_eval_count").toString();
            product.promotion_rate = goodsDetail.get("promotion_rate").toString();
            product.sold_quantity = goodsDetail.get("sold_quantity").toString();
            product.save();
            return product;
        }
        return product;
    }
}
